--
/**************************************************************************************************/
--Agregar los campos de auditoria idambdb, fechainsert, fechaupdate
/**************************************************************************************************/

ALTER TABLE afip.cndiva DROP COLUMN IF EXISTS idambdb; 
ALTER TABLE afip.cndiva DROP COLUMN IF EXISTS fechainsert; 
ALTER TABLE afip.cndiva DROP COLUMN IF EXISTS fechaupdate; 

ALTER TABLE afip.cndiva ADD COLUMN idambdb integer;
ALTER TABLE afip.cndiva ADD COLUMN fechainsert timestamp without time zone;
ALTER TABLE afip.cndiva ADD COLUMN fechaupdate timestamp without time zone;

DROP TRIGGER IF EXISTS control_audit_afip_cndiva ON afip.cndiva;
CREATE TRIGGER control_audit_afip_cndiva
    BEFORE INSERT OR DELETE OR UPDATE 
    ON afip.cndiva
    FOR EACH ROW
    EXECUTE FUNCTION public.tg_control_audit_instancia();
/**************************************************************************************************/


ALTER TABLE afip.cndivaempxcndivapad DROP COLUMN IF EXISTS idambdb; 
ALTER TABLE afip.cndivaempxcndivapad DROP COLUMN IF EXISTS fechainsert; 
ALTER TABLE afip.cndivaempxcndivapad DROP COLUMN IF EXISTS fechaupdate; 
	
ALTER TABLE afip.cndivaempxcndivapad ADD COLUMN idambdb integer;
ALTER TABLE afip.cndivaempxcndivapad ADD COLUMN fechainsert timestamp without time zone;
ALTER TABLE afip.cndivaempxcndivapad ADD COLUMN fechaupdate timestamp without time zone;

DROP TRIGGER IF EXISTS control_audit_afip_cndivaempxcndivapad ON afip.cndivaempxcndivapad;
CREATE TRIGGER control_audit_afip_cndivaempxcndivapad
    BEFORE INSERT OR DELETE OR UPDATE 
    ON afip.cndivaempxcndivapad
    FOR EACH ROW
    EXECUTE FUNCTION public.tg_control_audit_instancia();
/**************************************************************************************************/

ALTER TABLE afip.letras DROP COLUMN IF EXISTS idambdb; 
ALTER TABLE afip.letras DROP COLUMN IF EXISTS fechainsert; 
ALTER TABLE afip.letras DROP COLUMN IF EXISTS fechaupdate; 


ALTER TABLE afip.letras ADD COLUMN idambdb integer;
ALTER TABLE afip.letras ADD COLUMN fechainsert timestamp without time zone;
ALTER TABLE afip.letras ADD COLUMN fechaupdate timestamp without time zone;

DROP TRIGGER IF EXISTS control_audit_afip_letras ON afip.letras;
CREATE TRIGGER control_audit_afip_letras
    BEFORE INSERT OR DELETE OR UPDATE 
    ON afip.letras
    FOR EACH ROW
    EXECUTE FUNCTION public.tg_control_audit_instancia();
/**************************************************************************************************/

ALTER TABLE afip.ambdb DROP COLUMN IF EXISTS idambdb; 
ALTER TABLE afip.ambdb DROP COLUMN IF EXISTS fechainsert; 
ALTER TABLE afip.ambdb DROP COLUMN IF EXISTS fechaupdate; 

ALTER TABLE public.ambdb ADD COLUMN idambdb integer;
ALTER TABLE public.ambdb ADD COLUMN fechainsert timestamp without time zone;
ALTER TABLE public.ambdb ADD COLUMN fechaupdate timestamp without time zone;

DROP TRIGGER IF EXISTS control_audit_ambdb ON public.ambdb;
CREATE TRIGGER control_audit_ambdb
    BEFORE INSERT OR DELETE OR UPDATE 
    ON public.ambdb
    FOR EACH ROW
    EXECUTE FUNCTION public.tg_control_audit_instancia();
/**************************************************************************************************/

ALTER TABLE afip.stkdepfun DROP COLUMN IF EXISTS idambdb; 
ALTER TABLE afip.stkdepfun DROP COLUMN IF EXISTS fechainsert; 
ALTER TABLE afip.stkdepfun DROP COLUMN IF EXISTS fechaupdate; 

ALTER TABLE public.stkdepfun ADD COLUMN idambdb integer;
ALTER TABLE public.stkdepfun ADD COLUMN fechainsert timestamp without time zone;
ALTER TABLE public.stkdepfun ADD COLUMN fechaupdate timestamp without time zone;

DROP TRIGGER IF EXISTS control_audit_stkdepfun ON public.stkdepfun;
CREATE TRIGGER control_audit_stkdepfun
    BEFORE INSERT OR DELETE OR UPDATE 
    ON public.stkdepfun
    FOR EACH ROW
    EXECUTE FUNCTION public.tg_control_audit_instancia();
/**************************************************************************************************/
--arreglar PK de tabla
/**************************************************************************************************/
ALTER TABLE public.ambacncmp DROP CONSTRAINT IF EXISTS ambacncmp_pkey;
ALTER TABLE public.ambacncmp ADD CONSTRAINT ambacncmp_pkey PRIMARY KEY (id);
/**************************************************************************************************/

--   
SELECT aaversion('516.03','516.02');
--    



